<template>
  <div class="box">
    <el-form :model="form" label-width="120px" label-position="top">
      <el-form-item label="餐品名称">
        <el-input v-model="form.name" size="large" />
      </el-form-item>
      <el-form-item label="餐品价格">
        <el-input-number v-model="form.price" :precision="2" :step="10" />
      </el-form-item>
      <el-form-item label="餐品分类">
        <el-select v-model="form.type" class="m-2" placeholder="Select" size="large">
          <el-option label="主食" :value="0" />
          <el-option label="饮品" :value="1" />
        </el-select>
      </el-form-item>
      <el-form-item label="图片上传区">
        <el-upload
          v-model:file-list="fileList"
          action=""
          :multiple="false"
          :auto-upload="false"
          name="pic"
          :limit="5"
          list-type="picture-card"
          :on-change="change"
          :on-preview="handlePictureCardPreview"
        >
          <el-icon>
            <Plus />
          </el-icon>
        </el-upload>
        <el-dialog v-model="dialogVisible">
          <img w-full :src="dialogImageUrl" alt="Preview Image" />
        </el-dialog>
      </el-form-item>
      <el-form-item label="更新时间">
        <el-date-picker
          v-model="form.updateTime"
          type="date"
          placeholder="Pick a day"
          size="large"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit" class="btn">更新数据</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script lang="ts" setup>
import axios from '@/utils/request'
import { Plus } from '@element-plus/icons-vue'
import type { UploadProps, UploadUserFile } from 'element-plus'
import { onMounted, reactive, ref } from 'vue'
import { useDinnerStore } from '@/store/dinnerStore'
const dinnerStore = useDinnerStore()

let form = reactive({
  name: '',
  price: 100,
  type: 0,
  updateTime: '',
})
let id = ref<number>()
const fileList = ref<UploadUserFile[]>()
const dialogImageUrl = ref('')
const dialogVisible = ref(false)

onMounted(() => {
  let cover = [{ url: dinnerStore.currentData.cover }]
  id.value = dinnerStore.currentData.id
  form.name = dinnerStore.currentData.name
  form.price = dinnerStore.currentData.price
  fileList.value = cover as any
  form.type = dinnerStore.currentData.type
  form.updateTime = dinnerStore.currentData.updateTime as string
})

//更新餐品信息
const onSubmit = async () => {
  try {
    await axios.patch(`https://www.tinyxu.fun/v1/dinner/${id.value}`, {
      ...form,
    })
    ElMessage.success('上传成功')
  } catch (error) {
    console.log(error)
    ElMessage.error('出错了!上传失败')
  }
}

//更新图片
const change = async (file: any) => {
  const formData = new FormData()
  formData.append('cover', file.raw)
  try {
    await axios({
      url: `https://www.tinyxu.fun/v1/dinner/cover/${id.value}`,
      method: 'patch',
      data: formData,
      headers: { 'Content-Type': 'multipart/form-data' },
    })
    ElMessage.success('上传成功')
  } catch (error) {
    ElMessage.error('出错了!上传失败')
  }
}

const handlePictureCardPreview: UploadProps['onPreview'] = uploadFile => {
  dialogImageUrl.value = uploadFile.url!
  dialogVisible.value = true
}
</script>
<style lang="less" scoped>
.box {
  @media screen and (max-width: 1200px) {
    width: 100%;
    border-radius: 8px !important;
  }
  width: 70%;
  box-sizing: border-box;
  position: relative;
  .btn {
    width: 200px;
    height: 40px;
  }
}
</style>
